﻿#include "textedit.h"
#include <QFile>
#include <QFileInfo>
#include <QFileDialog>
#include <QTextDocumentWriter>
#include <QMessageBox>
#include <QCloseEvent>

TextEdit::TextEdit(QWidget*parent):QTextEdit(parent),docwindowTiele(""),docFilePath("")
{
    setAttribute(Qt::WA_DeleteOnClose);

}

int TextEdit::docNo=1;

TextEdit::~TextEdit()
{

}

void TextEdit::initNewDoc()
{
    docwindowTiele=QString("文档 %1").arg(docNo++);
    setWindowTitle(docwindowTiele+"[*]");

    connect(document(),&QTextDocument::contentsChanged,this,&TextEdit::setWindowModify);
}

QString TextEdit::getDocFilePath() const
{
    return this->docFilePath;
}

bool TextEdit::loadDoc(const QString &docName)
{
    if(!docName.isEmpty())
    {
        QFile file(docName);
        if(!file.exists())return false;
        if(!file.open(QFile::ReadOnly))return false;
        QByteArray text=file.readAll();

        if(Qt::mightBeRichText(text))
        {
            setHtml(text);
        }
        else
        {
            setPlainText(text);
        }

        initOpenDoc(docName);
    }
    return true;
}

QString TextEdit::getDocName() const
{
    return QFileInfo(docFilePath).fileName();
}

bool TextEdit::saveDoc()
{
    if(document()->isModified())
    {
        if(!docFilePath.isEmpty())
        {
            return writeToDoc(docFilePath);
        }
        else
        {
            return saveAsDoc();
        }
    }
    return false;
}

bool TextEdit::saveAsDoc()
{
    QString docName=QFileDialog::getSaveFileName(this,"另存为","../","文本文件(*.txt);;html文件(*.html)");
    if(docName.isEmpty())return false;
    return writeToDoc(docName);
}

QString TextEdit::getDocWindowTitle() const
{
    return docwindowTiele;
}

bool TextEdit::promptSave()
{
    if(!document()->isModified())return true;

    QMessageBox::StandardButton res;
    res=QMessageBox::warning(this,"提示",QString("文档%1已修改,是否保存?").arg(getDocWindowTitle()),
                             QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
    if(res==QMessageBox::Yes)
    {
        return saveDoc();
    }
    else if(res==QMessageBox::No)
    {
        return true;
    }
    else if(res==QMessageBox::Cancel)
    {
        return false;
    }
}

void TextEdit::closeEvent(QCloseEvent *event)
{
    if(promptSave())
    {
        event->accept();
    }
    else
    {
        event->ignore();
    }
}

void TextEdit::initOpenDoc(const QString&docName)
{
    docFilePath=QFileInfo(docName).canonicalFilePath();
    docwindowTiele=getDocName()+" "+QString::number(docNo++);
    setWindowTitle(docwindowTiele+"[*]");
    connect(document(),&QTextDocument::contentsChanged,this,&TextEdit::setWindowModify);
}

bool TextEdit::writeToDoc(const QString &docName)
{
    QTextDocumentWriter docWriter(docName);
    if(docWriter.write(this->document()))
    {
        docFilePath=QFileInfo(docName).canonicalFilePath();
        document()->setModified(false);
        setWindowModified(false);
    }
}



void TextEdit::setWindowModify()
{
    setWindowModified(document()->isModified());
}
